#include "form_tool_bar.h"
#include "ui_form_tool_bar.h"


enum EUM_BUTTON_IDX {BTN_HOME, BTN_TEST, BTN_SETTING, BTN_FACTORY};

/*
 * Function:    FormToolBar
 * Description: The default constructor
 * Paramer:     void
 * Return:
 */
FormToolBar::FormToolBar(QWidget *parent) :
    QWidget(parent)
  , ui(new Ui::FormToolBar)
{
    qDebug() << Q_FUNC_INFO;
    ui->setupUi(this);
}

/*
 * Function:    ~FormToolBar
 * Description: The default destructor
 * Paramer:     void
 * Return:
 */
FormToolBar::~FormToolBar()
{
    qDebug() << Q_FUNC_INFO;
    delete ui;
}

/*
 * Function:    getInstance
 * Description: Get an instance of "FormToolBar" class, if exist, return it
 * Paramer:     QWidget *pParent - the pointer of QWidget class
 * Return:      the pointer of "FormToolBar" class
 */
FormToolBar* FormToolBar::getInstance(QWidget *pParent)
{
    static FormToolBar *pNew = nullptr;
    if(!pNew) {
        // Create a new object if it doesn't exist
        pNew = new FormToolBar(pParent);
        if(nullptr == pNew) return nullptr;

        // After constructing object, initialize the object
        pNew->_initialze();

        // Init the window style
        pNew->_initUi();
    }

    // Set the parent object
    if(pParent) pNew->setParent(pParent);

    return pNew;
}

/*
 * Function:    _initialze
 * Description: After constructing object, initialize the object
 * Paramer:     void
 * Return:      void
 */
void FormToolBar::_initialze()
{
    // Signal-slot initialization
    std::vector<bool> vecConnect;
    for(int i=0; i<(int)vecConnect.size(); i++) {
        Q_ASSERT(vecConnect.at(i));
    }
}

/*
* Function:		_initUi
* Description:	Init the window style
* Paramer:		void
* Return:		void
*/
void FormToolBar::_initUi()
{
    // Displays the background image of the window
    QWidget::setAttribute(Qt::WA_StyledBackground);

    SET_STYLE_SHEET(QString(":/%1/home/%2.qss").arg("skin_1").arg(this->objectName()));

    ui->pushButton_Home->setFocusPolicy(Qt::NoFocus);
    ui->pushButton_Setting->setFocusPolicy(Qt::NoFocus);
    ui->pushButton_Factory->setFocusPolicy(Qt::NoFocus);

    ui->pushButton_Home->setCheckable(true);
    ui->pushButton_Setting->setCheckable(true);
    ui->pushButton_Factory->setCheckable(true);

    ui->pushButton_Home->setChecked(true);
}

void FormToolBar::on_pushButton_Home_clicked(bool checked)
{
    Q_UNUSED(checked);

    _buttonChecked(BTN_HOME);

    // Show test page
    emit signalShowPageHome();
}

void FormToolBar::on_pushButton_Setting_clicked(bool checked)
{
    Q_UNUSED(checked);

    _buttonChecked(BTN_SETTING);

    // Show setting page
    emit signalShowPageSetting();
}

void FormToolBar::on_pushButton_Factory_clicked(bool checked)
{
    Q_UNUSED(checked);

    _buttonChecked(BTN_FACTORY);

    // Show factory page
    emit signalShowPageFactory();
}

void FormToolBar::slotSetToolbarDisabled(bool bTesting)
{
    ui->pushButton_Home->setDisabled(bTesting);
    ui->pushButton_Setting->setDisabled(bTesting);

    if(bTesting) {
        ui->pushButton_Home->setChecked(false);
        ui->pushButton_Setting->setChecked(false);
    }
    else {
        ui->pushButton_Home->setChecked(true);
        ui->pushButton_Setting->setChecked(false);
    }
}

void FormToolBar::slotSetToolbarChecked(int nPageIdx)
{
    switch (nPageIdx) {
    case 0: this->on_pushButton_Home_clicked(true); break;
    case 2: this->on_pushButton_Setting_clicked(true); break;
    case 3: this->on_pushButton_Factory_clicked(true); break;
    default:
        break;
    }
}

/*
* Function:		changeEvent
* Description:	Triggers an event before it is switch language
* Paramer:		QShowEvent *e - the pointer of "QEvent"
* Return:		void
*/
void FormToolBar::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }

    QWidget::changeEvent(e);
}

/*
* Function:		_buttonChecked
* Description:	Clicked a function button
* Paramer:		int nIdx - button index
* Return:		void
*/
void FormToolBar::_buttonChecked(int nIdx)
{
    switch (nIdx) {
    case BTN_HOME:
        ui->pushButton_Home->setChecked(true);
        ui->pushButton_Setting->setChecked(false);
        ui->pushButton_Factory->setChecked(false);
        break;
    case BTN_SETTING:
        ui->pushButton_Home->setChecked(false);
        ui->pushButton_Setting->setChecked(true);
        ui->pushButton_Factory->setChecked(false);
        break;
    case BTN_FACTORY:
        ui->pushButton_Home->setChecked(false);
        ui->pushButton_Setting->setChecked(false);
        ui->pushButton_Factory->setChecked(true);
        break;
    default:
        break;
    }
}
